<template>
  <div>
    <el-form v-loading="loading"
             element-loading-text="正在登录..."
             element-loading-spinner="el-icon-loading"
             element-loading-background="rgba(0,0,0,0.8)"
             ref="loginForm"
             :model="loginForm"
             class="loginContainer">
      <h3 class="loginTitle">系统登录</h3>
      <el-form-item>
        <el-input type="text"
                  v-model="loginForm.account"
                  placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type="password"
                  v-model="loginForm.password"
                  auto-complete="false"
                  placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type="text"
                  auto-complete="false"
                  v-model="loginForm.captcha"
                  placeholder="点击更换验证码"
                  style="width:230px;margin-right:5px; vertical-align:middle;">
        </el-input>
        <img :src="captchaUrl"
             @click="updateCaptcha"
             style="vertical-align:middle;">
      </el-form-item>
      <el-checkbox v-model="checked"
                   class="loginRemember">记住我</el-checkbox>
      <el-button type="primary"
                 style="width:100%"
                 @click="submitLogin">登录</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>

export default {
  name: "Login",
  data() {
    return {
      captchaUrl: "/captcha?time=" + new Date(),
      loginForm: {
        account: "admin",
        password: "123456",
        captcha: "",
      },
      checked: false,
      loading: false,

    };
  },
  methods: {
    // 点击更新验证码
    updateCaptcha() {
      this.captchaUrl = "/captcha?time=" + new Date();
    },
    submitLogin() {
      this.$refs.loginForm.validate((validate) => {
        if (validate) {
          // 加载loading 加载动画
          this.loading = true;
          this.postRequest("/admin/api/login", {
            account: this.loginForm.account,
            password: this.loginForm.password,
            captcha: this.loginForm.captcha,

          }).then((resp) => {
            if (resp.code == 200 && resp.msg == "success") {
              console.log(resp.data)
              this.loading = false;
              //存储用户 
              window.sessionStorage.setItem("userInfo", JSON.stringify(resp.data));

              let path = this.$route.query.redirect
              this.$router.replace((path == "/" || path == undefined || path=="/login") ? '/home' : path)
            } else {
              this.loading = false;
              this.$message.error(resp.msg);
              return false;
            }
          }

          );
        } else {
          this.$message.error("请输入正确的用户名和密码");
        }
      });
    },
  },
};
</script>

<style lang="css" scoped>
.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.loginTitle {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

.loginRemember {
  text-align: left;
  margin: 0px 0px 15px 0px;
}

.el-form-item__content {
  display: flex;
  align-items: center;
}
</style>



